package com.sunrise.visual.frame.component
{
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.text.TextFormat;
	
	import mx.collections.ArrayCollection;
	import mx.collections.XMLListCollection;
	import mx.containers.Canvas;
	import mx.controls.Image;
	import mx.controls.Label;
	import mx.events.FlexEvent;
	
	import common.AnyChartFlexExt;
	
	public class ImageWithText extends Canvas
	{
		public function ImageWithText()
		{
			super();
		}
		
		private var _points:Array;
		private var oldPoints:ArrayCollection = new ArrayCollection();
		
		private var pointsChanged:Boolean = false;
		public function set points(arr:Array):void {
			if(arr == _points) return;
			oldPoints.addAll(new ArrayCollection(arr));
			this._points = arr;
			pointsChanged = true;
			invalidateProperties();
		}
		
		public var img:Image;
		private var _source:String;
		private var sourceChanged:Boolean = false;
		public function set source(str:String):void {
			if(str == _source) return;
			this._source = str;
			sourceChanged = true;
			this.invalidateDisplayList();
			this.invalidateProperties();
		}
		override protected function createChildren():void {
			super.createChildren();
			if(img== null) img = new Image();
			img.percentHeight = 100;
			img.percentWidth = 100;
			img.scaleContent = true;
			img.maintainAspectRatio = false;
			img.addEventListener(FlexEvent.UPDATE_COMPLETE, createCompleteHandler);
			this.addChild(img)
		}
		private function createCompleteHandler(e:Event):void {
			if(img.content != null) {
				contextW = img.content.width;
				contextH = img.content.height;
				this.invalidateDisplayList();
			}
		}
		
		override protected function commitProperties():void {
			super.commitProperties();
			if(sourceChanged) {
				sourceChanged = false;
				img.source = _source;
			}
			if(pointsChanged) {
				pointsChanged = false;
				removeOldChild();
				for each(var a:Object in _points) {
					if(a==null ||(a.x == 0 && a.y == 0)) {
					}  else {
						var ctl:Object = a.control;
						if(ctl != null && ctl is DisplayObject)  {
							this.addChild(ctl as DisplayObject);
						}
					}
				}
			}
		}
		private var contextW:Number = 0;
		private var contextH:Number = 0;
		
		override protected function updateDisplayList(width:Number, height:Number):void {
			super.updateDisplayList(width,height);
			var scaleW:Number = contextW == 0 || img.content == null ? 0: width/img.content.width;
			var scaleH:Number = contextH == 0 || img.content == null ? 0: height/img.content.height;
			var i:int = 0;
			for each(var a:Object in _points) {
				var dis:DisplayObject = a.control as DisplayObject;
				i++;
				if(dis != null) {
					var lblX:Number = scaleW * a.x;
					var lblY:Number = scaleH * a.y;
					if(scaleW != 0 && scaleH != 0 &&dis.height != 0 && dis.width != 0){
						if(dis is AnyChartFlexExt) {
							(dis as AnyChartFlexExt).setActualSize(dis.width * scaleW, dis.height * scaleH);
						}
					}
					dis.x = lblX;
					dis.y = lblY;
				}
			}
		}
		
		public function removeOldChild():void {
			if(oldPoints.length ==0) return;
			for each(var c:Object in oldPoints) {
				if(c.control != null && c.control is DisplayObject && 
					this.contains(c.control as DisplayObject)) {
					this.removeChild(c.control as DisplayObject);
					c.control = null;
					oldPoints.removeItemAt(oldPoints.getItemIndex(c));
				}
			}
		}
	}
}